﻿// Project: Salient
// http://salient.codeplex.com
// 
// Copyright 2010, Sky Sanders <sky at skysanders.net>
// Dual licensed under the MIT or GPL Version 2 licenses.
// http://salient.codeplex.com/license
// 
// Date: April 24 2010 

#region

using System;
using System.Data;
using System.IO;
using Microsoft.SqlServer.Types;
using NHibernate;
using NHibernate.SqlTypes;
using NHibernate.UserTypes;

#endregion

namespace Salient.SqlServer.NHibernate
{
    public class SqlGeographyUserType : IUserType
    {
        #region IUserType Members

        public object NullSafeGet(IDataReader rs, string[] names, object owner)
        {
            if (names.Length != 1)
                throw new InvalidOperationException("names array has more than one element. can't handle this!");

            object obj = NHibernateUtil.BinaryBlob.NullSafeGet(rs, names[0]);
            if (obj == null) return null;
            byte[] bytes = obj as byte[];

            if (bytes == null
                /*i know bytes are not null, but i don't like ignoring blue squigglies */
                || bytes.Length == 0)
            {
                return SqlGeography.Null;
            }

            SqlGeography returnValue = new SqlGeography();

            using (MemoryStream input = new MemoryStream(bytes))
            {
                using (BinaryReader r = new BinaryReader(input))
                {
                    returnValue.Read(r);
                }
            }

            return returnValue;
        }


        public void NullSafeSet(IDbCommand cmd, object value, int index)
        {
            IDataParameter parameter = (IDataParameter) cmd.Parameters[index];
            if (value == null)
                parameter.Value = DBNull.Value;
            else
            {
                SqlGeography obj = (SqlGeography) value;
                using (MemoryStream output = new MemoryStream())
                {
                    using (BinaryWriter w = new BinaryWriter(output))
                    {
                        obj.Write(w);
                    }
                    parameter.Value = output.ToArray();
                }
            }
        }

        public new bool Equals(object x, object y)
        {
            if (x == null || y == null)
                return false;

            SqlGeography xx = (SqlGeography) x;
            SqlGeography yy = (SqlGeography) y;
            return yy.ToString() == xx.ToString();
        }

        public int GetHashCode(object x)
        {
            return x.GetHashCode();
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public object DeepCopy(object value)
        {
            //SqlHierarchyId is struct so can just copy it

            SqlGeography toCopy = (SqlGeography) value;

            if (toCopy == SqlGeography.Null)
                return SqlGeography.Null;
            SqlGeography copy = new SqlGeography();

            using (MemoryStream s = new MemoryStream())
            {
                BinaryWriter w = new BinaryWriter(s);
                toCopy.Write(w);
                s.Seek(0, SeekOrigin.Begin);
                BinaryReader r = new BinaryReader(s);
                copy.Read(r);
            }

            return copy;
        }

        public object Replace(object original, object target, object owner)
        {
            throw new NotImplementedException();
        }

        public object Assemble(object cached, object owner)
        {
            return DeepCopy(cached);
        }

        public object Disassemble(object value)
        {
            return DeepCopy(value);
        }

        public SqlType[] SqlTypes
        {
            get { return new[] {NHibernateUtil.BinaryBlob.SqlType}; }
        }

        public Type ReturnedType
        {
            get { return typeof (SqlGeography); }
        }

        public bool IsMutable
        {
            get { return true; }
        }

        #endregion
    }
}